import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import RewardsModel 1.0

Rectangle {
    width: ListView.view.width
    height: 80
    color: index % 2 === 0 ? "#f0f0f0" : "#ffffff"
    border.color: "#cccccc"
    radius: 5

    required property int index
    required property string name
    required property int points

    property RewardsModel kidRewardsModel

    signal rewardExchanged(string message, string color)

    RowLayout {
        anchors.fill: parent
        anchors.margins: 10
        spacing: 10

        ColumnLayout {
            Layout.fillWidth: true
            spacing: 5

            Label {
                text: "奖励: " + name
                font.bold: true
            }

            Label {
                text: "所需积分: " + points
                color: userManager.availablePoints >= points ? "green" : "red"
            }

            Label {
                text: userManager.availablePoints >= points ? "可兑换" : "积分不足"
                color: userManager.availablePoints >= points ? "green" : "red"
                font.pixelSize: 12
            }
        }

        ColumnLayout {
            spacing: 5

            Button {
                text: "兑换"
                Layout.preferredWidth: 80
                enabled: userManager.availablePoints >= points
                onClicked: {
                    if (userManager.availablePoints >= points) {
                        userManager.pointsDecrease(points)
                        // 使用 ListView.view.model 来访问模型
                        kidRewardsModel.removeReward(index)
                        rewardExchanged("奖励兑换成功！消耗 " + points + " 积分", "green")
                    } else {
                        rewardExchanged("积分不足，无法兑换！", "red")
                    }
                }
            }

            Button {
                text: "删除"
                Layout.preferredWidth: 80
                onClicked: {
                    // 使用 ListView.view.model 来访问模型
                    kidRewardsModel.removeReward(index)
                    rewardExchanged("奖励删除成功！", "orange")
                }
            }
        }
    }
}
